package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.Chapter;
import com.ruoyi.system.mapper.ChapterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CourseMapper;
import com.ruoyi.system.domain.Course;
import com.ruoyi.system.service.ICourseService;

import javax.annotation.Resource;

/**
 * 课程Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-20
 */
@Service
public class CourseServiceImpl implements ICourseService {
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private ChapterMapper chapterMapper;

    /**
     * 查询课程
     *
     * @param id 课程主键
     * @return 课程
     */
    @Override
    public Course selectCourseById(Long id) {
        return courseMapper.selectCourseById(id);
    }

    /**
     * 查询课程列表
     *
     * @param course 课程
     * @return 课程
     */
    @Override
    public List<Course> selectCourseList(Course course) {
        return courseMapper.selectCourseList(course);
    }

    /**
     * 新增课程
     *
     * @param course 课程
     * @return 结果
     */
    @Override
    public int insertCourse(Course course) {
        return courseMapper.insertCourse(course);
    }

    /**
     * 修改课程
     *
     * @param course 课程
     * @return 结果
     */
    @Override
    public int updateCourse(Course course) {
        return courseMapper.updateCourse(course);
    }

    /**
     * 批量删除课程
     *
     * @param ids 需要删除的课程主键
     * @return 结果
     */
    @Override
    public int deleteCourseByIds(Long[] ids) {
        return courseMapper.deleteCourseByIds(ids);
    }

    /**
     * 删除课程信息
     *
     * @param id 课程主键
     * @return 结果
     */
    @Override
    public int deleteCourseById(Long id) {
        return courseMapper.deleteCourseById(id);
    }

    @Override
    public void format() {
        List<Chapter> chapters = chapterMapper.selectChapterList(new Chapter());
        Map<Long, List<Chapter>> collect = chapters.stream().collect(Collectors.groupingBy(Chapter::getCoid));
        collect.forEach((key, value) -> {
            List<Chapter> in = collect.get(key);
            if (in != null && !in.isEmpty()) {
                Chapter chapter = in.get(0);
                Long caid = chapter.getCaid();
                Long suid = chapter.getSuid();
                Long teid = chapter.getTeid();
                StringBuilder ss = new StringBuilder();
                in.forEach(item -> {
                    ss.append(item.getName()).append("<br/>");
                });
                Course course = new Course();
                course.setId(key);
                course.setCaid(caid);
                course.setSuid(suid);
                course.setTeid(teid);
                course.setContent(ss.toString());
                course.setName(chapter.getName());
                course.setImg("https://img.jinxingjiaopei.com/solely/1702369427390封面图最终.jpg");
                courseMapper.insertCourse(course);

            }
        });
    }
}
